// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;
using SnapObjects.Commons;

namespace SnapObjects.Data
{
    /// <summary>
    ///     Specifies that a property in the model class will use the 
    ///     specified model class to load and update data independently.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public class ModelEmbeddedAttribute : Attribute, IModelEmbedded
    {
        /// <summary>
        ///     Initializes a new instance of the ModelEmbeddedaAttribute class.
        /// </summary>
        /// <param name="modelType">The type of a model class which the property relies on to load and update data.</param>
        public ModelEmbeddedAttribute(Type modelType)
        {
            this.ModelType = modelType;

            this.QuerySelector = QuerySelector.Select;

            Check.NotNull(modelType, nameof(modelType));
        }

        /// <summary>
        ///     Gets the type of the model class which the property relies on to load and update data.
        /// </summary>
        public Type ModelType { get; }

        /// <summary>
        ///     Gets or sets the query method to be used when loading data for the current property.
        /// </summary>
        public QuerySelector QuerySelector { get; set; }

        /// <summary>
        ///     Sets the SQL expression parameter that corresponds to the query method of the QuerySelector property.
        /// </summary>
        public string QueryExpression { get; set; }

        /// <summary>
        ///     Gets or sets whether to perform the insert operation for the current property 
        ///     (using definitions of the detail model) when the current model is inserting data.
        /// </summary>
        public bool CascadeCreate { get; set; }

        /// <summary>
        ///     Gets or sets whether to perform the delete operation for the current property 
        ///     (using definitions of the detail model) when the current model is deleting data.
        /// </summary>
        public bool CascadeDelete { get; set; }

        /// <summary>
        ///     Gets or sets the value of the parameter that is required when loading the data for the current property.
        /// </summary>
        public string ParamValue { get; set; }

        /// <summary>
        ///     
        /// </summary>
        public bool IsQueryByKey
            => this.QuerySelector == QuerySelector.ScalarByKey ||
                  this.QuerySelector == QuerySelector.SelectByKey;
    }
}
